<?php
namespace Admin\Controller;
use Think\Controller;
class XuezuoController extends CommonController {
    /*两学一做列表页*/
    public function index(){
        $page = A('Admin/Page');
        $table  = 'xuezuo as a';
        $field  = 'a.*,b.name';
        $where  = "hd_user as b  on a.operator=b.id  and a.deletes = 1";
        $order  = 'id';
        $sum    = 20;
        $join   = 1;
        $info   = $page ->page($table, $field, $where, $order, $sum, $join);
        for($i = 0; $i < count($info[0]); $i++) {
            $zan = count(array_unique(explode(',', $info[0][$i][zan])));  //计算点赞人数
            $read = count(array_unique(explode(',', $info[0][$i][read])));  //计算浏览量
            $info[0][$i]['zans'] = $zan;
            $info[0][$i]['reads'] = $read;
        }
        $this->assign('list',$info[0]);// 赋值数据集
        $this->assign('page',$info[1]);// 赋值分页输出
        $this->display();
    }
    /*两学一做增加*/
    public function add(){

        if(IS_POST){
            $data['title']= $_POST['title'];
            $data['content']= $_POST['content'];
            $data['operator'] = $_SESSION['userid'];
            $data['time'] = time();
            if($_FILES['info_photo']){
                $upload=new \Think\Upload();
                $upload->maxSize=3145728; //文件上传的大小限制
                $upload->exts=array('jpg','gif','png','jpeg'); //上传文件后缀的样式
                $upload->rootPath  =      './Public/'; // 设置附件上传根目录
                $upload->savePath  =      '/uploads/';//保存目录
                $upload->saveName='time';//上传文件的名称
                $info=$upload->uploadOne($_FILES['info_photo']); //上传单文件
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $data['image'] =  $info['savepath'].$info['savename'];
                }
            }


            $result = M('xuezuo')->add($data);
            if(!$result){
                $this->error('文章发布失败');
            }else{
                $this->success('文章发布成功',U('Xuezuo/index'));
                exit;
            }
        }
        $this->display();
    }
    /*修改*/
    public function edit(){
        $id = I('get.sid');
        $xuezuo=M('xuezuo');
        $rs = $xuezuo->where("id = $id and deletes = 1")->find();
        $this->assign('rs',$rs);
        if(IS_POST){
            $data['title']= $_POST['title'];
            $data['content']= $_POST['content'];
            $data['operator'] = $_SESSION['userid'];
            $data['time'] = time();
            if($_FILES['info_photo']['error'] == 0){
                $upload=new \Think\Upload();
                $upload->maxSize=3145728; //文件上传的大小限制
                $upload->exts=array('jpg','gif','png','jpeg'); //上传文件后缀的样式
                $upload->rootPath  =      './Public/'; // 设置附件上传根目录
                $upload->savePath  =      '/uploads/';//保存目录
                $upload->saveName='time';//上传文件的名称
                $info=$upload->uploadOne($_FILES['info_photo']); //上传单文件
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $data['image'] =  $info['savepath'].$info['savename'];
                }
            }


            $result = M('xuezuo')->where("id=$id and deletes = 1")->save($data);
            if(!$result){
                $this->error('文章修改失败');
            }else{
                $this->success('文章修改成功',U('Xuezuo/index'));
                exit;
            }
        }
        $this->display();
    }
    /*删除*/
    public function del(){
        $id = I('post.id');
        $xuezuo=M('xuezuo');
        $data['deletes'] = 2;
        $rs=$xuezuo->where("id = $id")->save($data);
        if($rs){
            $this->ajaxReturn("ok");
        } else {
            $this->ajaxReturn("sss");
        }

    }
}